from wormbane.language import add_indefinite_article
from wormbane.Thing import Thing
import random


class Monster(Thing):

    size = 'medium'
    is_nemesis = False
    is_nemesis_nemesis = False
    min_exp_level = 1
    max_exp_level = 1

    def __init__(self, exp_level=None):

        Thing.__init__(self)

        if exp_level is None:
            self.exp_level = random.randrange(self.min_exp_level,
                                              self.max_exp_level + 1) 
        else:
            self.exp_level = exp_level

        self.items = []
        self.time = 0.0
        self.alive = True
        self.vitality = 1.0
        self.hunger = 0.0

        self.speed = 1.0
        self.protection = 1.0
        self.damage = 1.0

    @property
    def char(self):
        return (self.race[0].upper() if self.size in ('large', 'huge')
                else self.race[0])

    @property
    def vitality_percentage(self):
        return max(0, min(100, int(round(self.vitality * 100.0))))

    @property
    def vitality_shape(self):
        vitality = self.vitality
        if vitality < 0.2:
            return 'poor'
        elif vitality < 0.5:
            return 'fair'
        else:
            return 'good'

    @property
    def hungry(self):
        return self.hunger > 0.5

    def __str__(self):
        return add_indefinite_article('level %d %s'
                                      % (self.exp_level, self.race))

    @property
    def race(self):
        return type(self).__name__.lower()


class Dragon(Monster):
    color = 'red'
    size = 'huge'
    is_nemesis = True
    min_exp_level = 8
    max_exp_level = 10


class Drow(Monster):
    color = 'magenta'
    min_exp_level = 2
    max_exp_level = 4


class Dwarf(Monster):
    color = 'blue'
    size = 'small'
    min_exp_level = 2
    max_exp_level = 4


class Elf(Monster):
    color = 'green'
    min_exp_level = 2
    max_exp_level = 4


class Goblin(Monster):
    color = 'magenta'
    size = 'small'
    min_exp_level = 1
    max_exp_level = 3


class Hobbit(Monster):
    color = 'green'
    size = 'small'
    min_exp_level = 1
    max_exp_level = 3


class Human(Monster):
    color = 'yellow'
    min_exp_level = 2
    max_exp_level = 4


class Ogre(Monster):
    color = 'green'
    size = 'large'
    min_exp_level = 5
    max_exp_level = 7


class Orc(Monster):
    color = 'red'
    min_exp_level = 2
    max_exp_level = 4


class Troll(Monster):
    color = 'magenta'
    size = 'large'
    min_exp_level = 6
    max_exp_level = 8
